package com.quxing.amazon.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static com.quxing.amazon.common.StatusConst.*;


/**
 * @Author zhb
 * @Description
 * @Date 2020/5/16 11:26
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    private int code;
    private String msg;
    private T data;


    private static Result error = new Result<>(ERROR_CODE, ERROR, null);

    private static Result success = new Result<>(SUCCESS_CODE, SUCCESS, null);

    public static Result success() {
        return success;
    }

    public static Result error() {
        return error;
    }

    public static Result error(String msg) {
        return of(ERROR_CODE, msg, null);
    }

    public static <T> Result<T> of(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }

    public static <T> Result<T> success(T data) {
        return of(SUCCESS_CODE, SUCCESS, data);
    }

    public static Result successMsg(String msg) {
        return of(SUCCESS_CODE, msg, null);
    }


    public static Result exception(BaseException e) {
        return of(ERROR_CODE, e.getMsg(), null);
    }
}
